#include <Windows.h>
#include <tchar.h>

//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{	
	static const UINT ID_BTNSTARTUP	= 1001;
	static const UINT ID_BTNFIND	= 1002;
	
	switch (nMsg)
	{
		case WM_CREATE:
		{
			//--: Move Main wnd to center of screen
			RECT rtMainWnd;
			GetClientRect(hWnd, &rtMainWnd);
			UINT nSrnWidth		= GetSystemMetrics(SM_CXFULLSCREEN);
			UINT nSrnHeight		= GetSystemMetrics(SM_CYFULLSCREEN);
			UINT nWndWidth		= (rtMainWnd.right - rtMainWnd.left);
			UINT nWndHeight		= (rtMainWnd.bottom - rtMainWnd.top);
			UINT nLeft			= (nSrnWidth - nWndWidth) / 2;
			UINT nTop			= (nSrnHeight - nWndHeight) / 2;
			MoveWindow(hWnd, nLeft, nTop, nWndWidth, nWndHeight, TRUE);

			CreateWindow(_T("Button"), _T("Startup NotePad"), WS_VISIBLE|WS_CHILD, 0, 0, 150, 50, hWnd, (HMENU)ID_BTNSTARTUP, 0, 0); 
			CreateWindow(_T("Button"), _T("Find && Rename"), WS_VISIBLE|WS_CHILD, 200, 0, 150, 50, hWnd, (HMENU)ID_BTNFIND, 0, 0); 

			break;
		}
		case WM_COMMAND:
		{
			switch (wParam)
			{
				case ID_BTNSTARTUP:
				{
					ShellExecute(hWnd, _T("Open"), _T("Notepad.exe"), NULL, NULL, SW_SHOW);
					break;
				}
				case ID_BTNFIND:
				{
					HWND hWndNotePad = FindWindow(_T("Notepad"), NULL);
					if (NULL != hWndNotePad && IsWindow(hWndNotePad))
					{
						SetWindowText(hWndNotePad, _T("I come from Demo0024"));
						
						HWND hWndEdit = FindWindowEx(hWndNotePad, NULL, _T("Edit"), NULL);
						if (NULL != hWndEdit && IsWindow(hWndEdit))
						{
							const TCHAR* szText = _T("This string is writed by Demo0024 program");
							SendMessage(hWndEdit, WM_SETTEXT, (WPARAM)_tcslen(szText) * sizeof(TCHAR), (LPARAM)szText);

							MessageBox(hWndNotePad, _T("Demo0024 rename for me"), _T("I come from Demo0024"), MB_OK);
						}
					} else {
						MessageBox(hWnd, _T("Can't find notepad"), _T("Error"), MB_OK);
					}
					break;
				}
			}
			break;
		}
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////////
bool RegisterWndClass(HINSTANCE hInstance, const TCHAR* szClsName)
{
	WNDCLASSEX wcs		= {0};
	wcs.cbSize			= sizeof(WNDCLASSEX);
	wcs.hbrBackground	= (HBRUSH)GetStockObject(COLOR_APPWORKSPACE);
	wcs.lpszClassName	= szClsName;
	wcs.lpfnWndProc		= WndProc;
	wcs.hCursor			= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wcs.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcs.style			= CS_VREDRAW|CS_HREDRAW;
	
	return (0 != RegisterClassEx(&wcs));
}

//////////////////////////////////////////////////////////////////////////
bool CreateMainWnd(HINSTANCE hInstance, const TCHAR* szClsName, const TCHAR* szWndCaption)
{
	if (NULL != szClsName && NULL != szWndCaption)
	{
		HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, szClsName, szWndCaption, WS_VISIBLE|WS_POPUPWINDOW|WS_CAPTION, 
								0, 0, 800, 480, NULL, NULL, hInstance, hInstance);
		if (NULL != hWnd && IsWindow(hWnd))
		{
			ShowWindow(hWnd, SW_SHOW);
			UpdateWindow(hWnd);
			return true;
		}
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
void RunMessageLoop()
{
	MSG Msg;
	while (true)
	{
		while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (WM_QUIT == Msg.message)
			{
				return;
			}

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
}
//////////////////////////////////////////////////////////////////////////
BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szClsName		= _T("Win32App");
	const TCHAR* szWndCaption	= _T("Demo to Find Wnd");

	if (RegisterWndClass(hInstance, szClsName))
	{
		if (CreateMainWnd(hInstance, szClsName, szWndCaption))
		{
			RunMessageLoop();
		}
	}

	return TRUE;
}